package com.stockz.portfolio.domain.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Transient;

import com.stockz.core.model.Scrip;

public class Portfolio {

	private String name;
	private Date createDate;
	private double startingAmount;
	private List<Transaction> transactions;
	private List<Holding> holdings;
	
	@Transient
	private double currentValue;
	@Transient
	private boolean dirty = true;
	public boolean isDirty() {
		return dirty;
	}
	public void setDirty(boolean dirty) {
		this.dirty = dirty;
	}
	public double getCurrentValue() {
		return currentValue;
	}
	public void setCurrentValue(double value){
		this.currentValue = value;
	}
	
	public Portfolio() {
		
	}
	
	public Portfolio(String name, double startingAmount) {
		super();
		this.name = name;
		this.startingAmount = startingAmount;
		this.createDate = new Date();
		transactions = new ArrayList<Transaction>();
		holdings = new ArrayList<Holding>();
	}
	
	public void reset(){
		this.startingAmount = 0;
		this.createDate = new Date();
		transactions.clear();
		holdings.clear();
	}
	
	public void reset(double startingAmount){
		this.startingAmount = startingAmount;
		this.createDate = new Date();
		transactions.clear();
		holdings.clear();
	}
	
	public Holding getHolding(Scrip scrip){
		if(getHoldings() != null && scrip != null){
			if(getHoldings() != null){
				for(Holding holding1 : getHoldings()){
					if(holding1.getScrip().equals(scrip)){
						return holding1;
					}
				}
				Holding holding = new Holding();
				getHoldings().add(holding);
				return holding;
			}
		}
		return null;
	}
	
	public void validate(Transaction transaction){
		
	}
	
	public String getName() {
		return name;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public double getStartingAmount() {
		return startingAmount;
	}
	public List<Transaction> getTransactions() {
		return transactions;
	}
	public List<Holding> getHoldings(){
		return holdings;
	}
	
}
